package org.one.afternoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.Customers;
import org.one.afternoon.service.CustomersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/Customers")
public class CustomersController {

    @Autowired
    private CustomersService customersService;

    @GetMapping("/getAll")
    @ResponseBody
    public Result<Customers> GetCustomers(){

        QueryWrapper<Customers> customersQueryWrapper = new QueryWrapper<Customers>().eq("id",1);
        return Result.success(customersService.getOne(customersQueryWrapper));

    }

    @PostMapping("/update")
    @ResponseBody
    public Result<String> UpdateCustomers(@RequestBody Customers customers){

        UpdateWrapper<Customers> customersUpdateWrapper = new UpdateWrapper<Customers>()
                .set("name",customers.getName())
                .set("phone_number",customers.getPhoneNumber())
                .set("post_name",customers.getPostName())
                .set("address",customers.getAddress())
                .set("avatar",customers.getAvatar());
        if (customersService.update(customersUpdateWrapper)){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }
}
